package Tasks;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import controller.WebServiceController;
import model.ProductItem;
import android.R;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;

public class GetProductItemAsync extends AsyncTask<Context,Void,ArrayAdapter<ProductItem>> {

	@Override
	protected ArrayAdapter<ProductItem> doInBackground(Context... arg0) 
	{
		WebServiceController controller=new WebServiceController();
		ArrayList<ProductItem> productList=new ArrayList<ProductItem>();
		ArrayAdapter<ProductItem> adapter=new ArrayAdapter<ProductItem>(arg0[0],R.layout.simple_list_item_1);
		try {
			productList=controller.GetProductItems();
			for (ProductItem productItem : productList) 
			{
				adapter.add(productItem);
			}
			
			return adapter;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}
